﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;


using HtmlAgilityPack;
using System.Threading;
namespace SmartCollection
{
    public partial class Form1 : Form
    {
        TheGioiPhaiDep_DataEntities db = new TheGioiPhaiDep_DataEntities();
        SmartCollectionLoad smartLoad = new SmartCollectionLoad();
        public Form1()
        {
            InitializeComponent();
        }
        void ReadFile(string s)
        {

        }
        void WriteFile(string s)
        {
            if (!File.Exists(@"e:\pass.txt"))//kiểm tra nếu chưa có file Pass.txt thì tạo ra file Pass.txt
            {
                FileStream fs;
                fs = new FileStream(@"e:\pass.txt", FileMode.Create);
                StreamWriter sWriter = new StreamWriter(fs, Encoding.UTF8);

                sWriter.WriteLine(s);
                sWriter.Flush();
                fs.Close();
            }
        }
        private delegate void AddCallback(NewsDetails item);
        private void AddItem(NewsDetails item)
        {
            if (this.listView1.InvokeRequired)
            {
                this.listView1.BeginInvoke(new AddCallback(AddItem), (object)item);
            }
            else
            {


              
              
            
                    ListViewItem lvi = new ListViewItem(item.Title);
                    lvi.SubItems.Add(item.Url);
                    lvi.SubItems.Add(item.CategoryNewsId);
                    this.listView1.Items.Add(lvi);
                


            }
            //treeView1.Refresh();

        }
        public void Check()
        {

            List<Menu> ListMenu = new List<Menu>();
            List<NewsDetails> ListDetail = new List<NewsDetails>();
            List<News> ListNews = new List<News>();
            List<NumberPage> ListPage = new List<NumberPage>();
            string urlGoc = "http://afamily.vn";
            ListMenu = smartLoad.LoadMenu("http://afamily.vn/dep.chn", "//ul[@class='mgt5']//li", "", urlGoc);




          

            for (int i = 0; i < ListMenu.Count(); i++)
            {
                ListPage = smartLoad.LoadPage(ListMenu[i], "//div[@class='paging fl mgb20']//a", "http://afamily.vn/", urlGoc);
                for (int j = 0; j < ListPage.Count(); j++)
                {

                    ListDetail.AddRange(smartLoad.LoadHomeNews(ListPage[j], "//div[@class='list-news1']//div[@class='box-x2']", "", "//div//h2//a", "//div//p", "//div//a", "http://afamily.vn/", urlGoc));
                    ListDetail.AddRange(smartLoad.LoadHomeNews(ListPage[j], "//div[@class='list-news1']//div[@class='box-x1']", "", "//h2//a", "//p", "//a", "http://afamily.vn/", urlGoc));
                    ListDetail.AddRange(smartLoad.LoadHomeNews(ListPage[j], "//div[@class='list-news1']//div[@class='box-x3']", "", "//div//h2//a", "//div//p", "//div//a", "http://afamily.vn/", urlGoc));



                    for (int k = 0; k < ListDetail.Count(); k++)
                    {


                       var item =  smartLoad.LoadNews(ListDetail[k], "//div[@class='detail_content fl mgt15']");
                       if (item != null)
                       {

                           AddItem(item);
                         
                       }
                    
                        //if (dem == 30)
                        //    break;
                    }


                    //if (dem == 30)
                    //    break;
                }
                //if (dem == 30)
                //    break;

            }
            System.Windows.Forms.MessageBox.Show("\nThanh cong!");
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");


    



        }

        public void Update()
        {

            List<Menu> ListMenu = new List<Menu>();
        
            UpdateNews updatenews = new UpdateNews();
            List<NumberPage> ListPage = new List<NumberPage>();
            string urlGoc = "http://afamily.vn";
            ListMenu = smartLoad.LoadMenu("http://afamily.vn/dep.chn", "//ul[@class='mgt5']//li", "", urlGoc);




            int dem = 0; 

            for (int i = 0; i < ListMenu.Count(); i++)
            {
                ListPage = smartLoad.LoadPage(ListMenu[i], "//div[@class='paging fl mgb20']//a", "http://afamily.vn/", urlGoc);
                for (int j = 0; j < ListPage.Count(); j++)
                {
                    List<NewsDetails> ListDetail = new List<NewsDetails>();
                   var load1= smartLoad.LoadUpdateNewHomeNews(ListPage[j], "//div[@class='list-news1']//div[@class='box-x2']", "", "//div//h2//a", "//div//p", "//div//a", "http://afamily.vn/", urlGoc, "//div//span");
                  var load2 = smartLoad.LoadUpdateNewHomeNews(ListPage[j], "//div[@class='list-news1']//div[@class='box-x1']", "", "//h2//a", "//p", "//a", "http://afamily.vn/", urlGoc, "//span");
                  var load3 = smartLoad.LoadUpdateNewHomeNews(ListPage[j], "//div[@class='list-news1']//div[@class='box-x3']", "", "//div//h2//a", "//div//p", "//div//a", "http://afamily.vn/", urlGoc, "//div//span");

               
                  ListDetail.AddRange(load1.ListNewsDetails);
                  ListDetail.AddRange(load2.ListNewsDetails);
                  ListDetail.AddRange(load3.ListNewsDetails);
                    for (int k = 0; k < ListDetail.Count(); k++)
                    {
                        var item = smartLoad.LoadNews(ListDetail[k], "//div[@class='detail_content fl mgt15']");
                        if (item != null)
                        {
                            dem++;

                            AddItem(item);

                        }

                        //if (dem == 30)
                        //    break;
                    }

                    if (load1.flag == true || load2.flag == true || load3.flag == true)
                    {
                        break;
                    }

                    //if (dem == 30)
                    //    break;
                }
                //if (dem == 30)
                //    break;

            }
            System.Windows.Forms.MessageBox.Show("\nThanh cong!Co " + dem + " tin moi");
            //if (dem == 0)
            //{
            //    MessageBox.Show("\nKhong co san pham moi!");
            //}
            //else MessageBox.Show("\nCo "+dem + " san pham moi");






        }
        private void listBox1_SelectedValueChanged(object sender, EventArgs e)
        {

            ThoiTrangDanny thoitrangdanny =new ThoiTrangDanny() ;
        

          //  string s = "http://thoitrangdanny.com/product/catalog/39-HANG-QUANG-CHAU-2014---CO-SAN.html";

            int SelectedIndex = listBox1.SelectedIndex;
            string SelectedValue = listBox1.SelectedItem.ToString();
           // MessageBox.Show(SelectedItem);
            //thoitrangdanny.LoadProduct("http://thoitrangdanny.com/product/chi-tiet/3859-Dam-voan-lua-min-30008-mau-xanh-nhu-hinh-freesize-.html");
            //load tai thoi trang danny
            //if (SelectedIndex == 0)
            //{
            //    Thread secondThread;
            //    ThreadStart starter = () => thoitrangdanny.Run(SelectedValue, 4, 42);
            //    secondThread = new Thread(starter);
            //    secondThread.Start();
            //}
            //else if (SelectedIndex == 1)
            //{
            //    Thread secondThread;
            //    ThreadStart starter = () => thoitrangdanny.Run(SelectedValue, 4, 41);
            //    secondThread = new Thread(starter);
            //    secondThread.Start();
            //}
            //else 


            //if (SelectedIndex == 2)
            //{
            //    Tapchi_Guu guu = new Tapchi_Guu();
            //    List<CategoryNewsUrl> arrayLink = new List<CategoryNewsUrl>();
            //   // guu.BasicLoad("http://tapchi.guu.vn/", 1, "dg", "cvx", arrayLink);

            //    List<PageUrlNews> list = new List<PageUrlNews>();
            ////    guu.Run("http://tapchi.guu.vn/");

            //    Thread secondThread;
            //    ThreadStart starter = () => guu.Run("http://tapchi.guu.vn/");
            //    secondThread = new Thread(starter);
            //    secondThread.Start();


            //   // guu.UrlNewsLoad("http://tapchi.guu.vn/phoi-do-thoi-trang", list);
            //}
           // load trang fake

            // MessageBox.Show("Thanh cong");
            if (SelectedIndex == 2)
            {

                Afamilyvn avn = new Afamilyvn();
                Thread secondThread;
              
                if (rdbLoadnews.Checked)
                {
                    this.listView1.Items.Clear();
               
                    this.listView1.View = View.Details;
                    this.listView1.FullRowSelect = true;

                    ThreadStart starter = () => Check();
                    secondThread = new Thread(starter);
                    secondThread.Start();
                  //  Check();
                   
               
                }
                else if (rdbUpdate.Checked)
                {
                    this.listView1.Items.Clear();

                    this.listView1.View = View.Details;
                    this.listView1.FullRowSelect = true;

                    ThreadStart starter = () => Update();
                    
                    secondThread = new Thread(starter);
                    secondThread.Start();
                  
                }
            }
        }

        private void panel1_Paint(object sender, PaintEventArgs e)
        {

        }

    }
}
